package request.share;

public class Operate
{
    private static Operate instance=null;

    private Operate()
    {//{{{
    }//}}}

    public synchronized static Operate getInstance()
    {//{{{
        if(instance == null)
        {
            instance=new Operate();
        }
        return instance;
    }//}}}

    public void init()
    {/*{{{*/
        this.shutdown=false;

        Cnfg cnfg=Cnfg.getInstance();
        this.workers=new Worker[cnfg.workers];
        for(int i=0; i<cnfg.workers; ++i)
        {
            this.workers[i]=new Worker();
            this.workers[i].status=ThreadStatus.RUNNING;
        }
    }/*}}}*/

    private boolean shutdown;
    public static enum ThreadStatus { RUNNING, STOPPED };

    private class Worker
    {//{{{
        ThreadStatus status; 
    }//}}}
    private Worker[] workers;

    public synchronized void shutdown()
    {//{{{
        this.shutdown=true;
    }//}}}

    public synchronized boolean isShutdown()
    {//{{{
        return this.shutdown;
    }//}}}

    public synchronized boolean isWorkerRunning(int worker_idx)
    {//{{{
        return this.workers[worker_idx].status == ThreadStatus.RUNNING;
    }//}}}

    public synchronized void workerStop(int worker_idx)
    {//{{{
        this.workers[worker_idx].status=ThreadStatus.STOPPED;
    }//}}}
}
